const express=require('express');
const expressStatic=require('express-static');
const slideRouter=require('./routers/slideRouter');
const dbConnect=require('./utils/db/mongodbConnect');
const userRouter=require('./routers/userRouter');
const childRouter=require('./routers/childRouter');
const heRouter=require('./routers/heRouter');
const sheRouter=require('./routers/sheRouter');
const mobRouter=require('./routers/mobRouter');
const mainRouter=require('./routers/mainRouter');
const carRouter=require('./routers/carRouter');
const bodyParser=require('body-parser');
const cookieParser=require('cookie-parser');
const cookieSession=require('cookie-session');
const viewsRouter=require('./routers/viewsRouter');

var app=express();
app.listen(8083,function (err) {
    if(!err){
        console.log('服务器启动成功');
    }else{
        console.log('服务器出错！');
    }
})
dbConnect.dbConnect();
//路由前 用body-parser;
app.use(bodyParser.urlencoded({extend:true}));
//cookie
//生成keys
var keys=[];
for(var i=0;i<100;i++){
    var str='abcd123456%%^&'+Math.random(100000);
    keys.push(str);
}
app.use(cookieParser());
app.use(cookieSession({
    name:'session',
    keys:keys,
    maxAge:20*60*1000
}))


//启用路由
app.use('/user',userRouter.router);
app.use('/slide',slideRouter.router);
app.use('/views',viewsRouter.router);
app.use('/child',childRouter.router);
app.use('/he',heRouter.router);
app.use('/she',sheRouter.router);
app.use('/mob',mobRouter.router);
app.use('/main',mainRouter.router);
app.use('/car',carRouter.router);
//起始页
app.use('/',function (req,res,next) {
    var options = {
        root: __dirname + '/www/',
        dotfiles: 'deny',
        headers: {
            'x-timestamp': Date.now(),
            'x-sent': true
        }
    };
    var url=req.url;
    //根文件夹
    // console.log(url.lastIndexOf('/')+'--'+url.length);
    if(url.indexOf('/')==0 && url.length>1){
        next();
    }else{
        res.sendFile('index.html',options,function (err) {
            if(err){
                console.log('index.html is error!');
            }
        });
    }
});

app.use('/favicon.ico',function (req,res) {
    res.send('favicon.ico');
})



app.use(expressStatic('./www'));

